package com.htj.powerbank.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.htj.powerbank.entity.User;
import com.htj.powerbank.entity.User_pm;
import com.htj.powerbank.mapper.UserMapper;
import com.htj.powerbank.service.UserService;
import com.htj.powerbank.vo.PageRequest;
import com.htj.powerbank.vo.PageResult;
import com.htj.powerbank.vo.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;

    @Override
    public User getUserByName(String name) {
        return userMapper.selectUserByName(name);
    }

    @Override
    public int addUser(User user){
        return userMapper.addUser(user);
    }

    @Override
    public User getUserById(Integer id){
        return userMapper.selectUserById(id);
    }

    @Override
    public PageResult selectUserInfoListBy(PageRequest pageRequest, User user) {
        return PageUtils.getPageResult(pageRequest, getPageInfo(pageRequest,user));
    }

    @Override
    public int updateUser(User user) {
        return userMapper.updateUserBy(user);
    }

    @Override
    public int updateUserStatus(Integer id, String status) {
        return userMapper.unpdateStatusBy(id,status);
    }

    public PageInfo<User> getPageInfo(PageRequest pageRequest, User user) {
        int pageNum = pageRequest.getPageNum();
        int pageSize = pageRequest.getPageSize();
        PageHelper.startPage(pageNum,pageSize);
        List<User> userInfos = userMapper.selectUserInfoListByPage(user);
        PageInfo<User> pageInfo = new PageInfo<>(userInfos);
        return pageInfo;
    }
}
